<template>
  <el-form :model="step" ref="form" :rules="rules" label-width="120px">
    <el-form-item label="步骤名称" prop="name">
      <el-input v-model="step.name" placeholder="请输入步骤名称"></el-input>
    </el-form-item >
    <el-form-item label="数据源选择">
      <el-row>
        <el-col :span="8">
          <el-form-item prop="connection">
            <el-select v-model="step.connection" placeholder="目标数据源">
              <el-option v-for="source in dataSource.sourceList" :label="source.name" :value="source.id">
                <span style="float: left">{{ source.name }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">{{ source.type }}</span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="1">
          <el-form-item prop="table">
            <el-select v-model="step.table" placeholder="目标表..">
              <el-option v-for="table in dataSource.tables" :label="table.cnname" :value="table.name"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form-item>
    <el-form-item label="提交记录数" style="margin-bottom: 0">
      <el-input-number v-model="step.commit" :min="0" :step="1000"></el-input-number>
    </el-form-item>
    <el-form-item label="不执行任何更新">
      <el-checkbox v-model="step.update_bypassed" true-label="Y" false-label="N"
                   style="padding-right: 50px"></el-checkbox>
    </el-form-item>
    <el-button-group>
      <el-button icon="plus" @click="addQuery" >查询字段</el-button>
      <el-button icon="plus" @click="addUpdate" style="margin-left:25px ">更新字段</el-button>
      <el-button type="primary" @click="fetchFieldsFromTable(transportFields)">获取更新字段</el-button>
    </el-button-group>
    <br><br>
    <el-table :data="step.queryList" stripe border :max-height="150">
      <el-table-column type="index" :width="60"></el-table-column>
      <el-table-column label="名称" :width="120">
        <template scope="scope">
          <div v-if="!scope.row.edit">{{scope.row.field}}</div>
          <div v-else>
            <el-select v-model="scope.row.field">
              <el-option v-for="item in selectedTable.columnList" :value="item.name"></el-option>
            </el-select>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="比较符" :width="120">
        <template scope="scope">
          <div v-if="!scope.row.edit">{{scope.row.condition}}</div>
          <div v-else class="control">
            <el-select v-model="scope.row.condition">
              <el-option v-for="item in options.whereCondition" :value="item"></el-option>
            </el-select>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="流字段A" :width="120">
        <template scope="scope">
          <div v-if="!scope.row.edit">{{scope.row.name}}</div>
          <div v-else class="control">
            <el-select v-model="scope.row.name">
              <el-option v-for="item in step.availableFields" :value="item.name"></el-option>
            </el-select>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="流字段B" :width="120">
        <template scope="scope">
          <div v-if="!scope.row.edit">{{scope.row.name2}}</div>
          <div v-else class="control">
            <el-select v-model="scope.row.name2">
              <el-option v-for="item in step.availableFields" :value="item.name"></el-option>
            </el-select>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="控制" :width="140">
        <template scope="scope">
          <el-row>
            <el-col :span="12">
              <el-button size="small" type="info" @click="reverseEditState(scope.$index, scope.row)">编辑</el-button>
            </el-col>
            <el-col :span="12">
              <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row,step.queryList)">删除
              </el-button>
            </el-col>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <br>
    <el-table :data="step.updateList" stripe border :max-height="150">
      <el-table-column type="index" :width="60"></el-table-column>
      <el-table-column label="表字段" :width="190">
        <template scope="scope">
          <div v-if="!scope.row.edit">{{scope.row.name}}</div>
          <div v-else>
            <el-select v-model="scope.row.name">
              <el-option v-for="item in selectedTable.columnList" :value="item.name"></el-option>
            </el-select>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="流字段" :width="190">
        <template scope="scope">
          <div v-if="!scope.row.edit">{{scope.row.rename}}</div>
          <div v-else class="control">
            <el-select v-model="scope.row.rename">
              <el-option v-for="item in step.availableFields" :value="item.name"></el-option>
            </el-select>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="更新" :width="100">
        <template scope="scope">
          <div v-if="!scope.row.edit">{{scope.row.update}}</div>
          <div v-else class="control">
            <el-select v-model="scope.row.update">
              <el-option value="Y"></el-option>
              <el-option value="N"></el-option>
            </el-select>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="控制" :width="140">
        <template scope="scope">
          <el-row>
            <el-col :span="12">
              <el-button size="small" type="info" @click="reverseEditState(scope.$index, scope.row)">编辑</el-button>
            </el-col>
            <el-col :span="12">
              <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row,step.updateList)">删除
              </el-button>
            </el-col>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
  </el-form>
</template>
<style>

</style>
<script>
  import {Steps, Fields, Options} from "../../model/DataModel";
  import StepCommon from "./mixins/StepMixin";
  import DataSourceCommon from "./mixins/DataSourceMixin";

  export default{
    mixins: [StepCommon, DataSourceCommon],
    data(){
      return {
        step: Steps.InsertUpdate(),
        options: Options
      }
    },
    methods: {
      transportFields(unique, table){
        this.step.updateList = table.columnList.map(el => ({
          name: el.name,
          rename: unique[el.name.toUpperCase()],
          update: "Y",
          edit: false
        }))
      },
      addUpdate(){
        this.step.updateList.push(Fields.InsertUpdate({edit: false, type: "update"}));
      },
      addQuery(){
        this.step.queryList.push(Fields.InsertUpdate({edit: false}));
      }
    }
  }
</script>
